{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime, timedelta\n",
    "from decimal import Decimal\n",
    "from math import modf\n",
    "\n",
    "class JNum(int):\n",
    "    def __truediv__(self, other_frac):\n",
    "        result = float(self.__value__) / float(other_frac)\n",
    "        return JNum(int(result))\n",
    "    def __init__(self, num):\n",
    "        self.__value__ = num\n",
    "    def __add__(self, other_frac):\n",
    "        return JNum(self.__value__ + other_frac)\n",
    "    def __radd__(self, other_frac):\n",
    "        return JNum(self.__value__ + other_frac)\n",
    "    def __mul__(self, other_frac):\n",
    "        return JNum(self.__value__ * other_frac)\n",
    "    def __rmul__(self, other_frac):\n",
    "        return JNum(self.__value__ * other_frac)\n",
    "    def __sub__(self, other_frac):\n",
    "        return JNum(self.__value__ - other_frac)\n",
    "    def __rsub__(self, other_frac):\n",
    "        return JNum(other_frac - self.__value__)\n",
    "    def __neg__(self):\n",
    "        return JNum(-self.__value__)\n",
    "\n",
    "def JulianDate(DateAndTime: datetime)-> Decimal:\n",
    "    y, m, d = JNum(DateAndTime.year), JNum(DateAndTime.month), JNum(DateAndTime.day)\n",
    "    J = 367 * y - (7 * (y + (m + 9) / 12)) / 4 - (3 * ((y + (m - 9) / 7) / 100 + 1 )) / 4 + 275 * m / 9 + d + 1721029\n",
    "    h, m, s = Decimal(DateAndTime.hour), Decimal(DateAndTime.minute), Decimal(DateAndTime.second)\n",
    "    J = Decimal(int(J))\n",
    "    timedelta_in_seconds = (h * 3600 + m * 60 + s) - (12 * 3600)\n",
    "    timedelta_in_one = timedelta_in_seconds / Decimal(3600 * 24)\n",
    "    J += timedelta_in_one\n",
    "    return J\n",
    "\n",
    "\n",
    "def RJulianDate(J: Decimal)->datetime:\n",
    "    JTime, JD = modf(J)\n",
    "    JTime, JD = Decimal(JTime), JNum(JD)\n",
    "    a = JD + 68569\n",
    "    b = 4 * a / 146097\n",
    "    c = a - (146097 * b + 3) / 4\n",
    "    d = (4000 * (c + 1)) / 1461001\n",
    "    e = c - (1461 * d) / 4 + 31\n",
    "    f = (80 * e) / 2447\n",
    "    day = e - (2447 * f) / 80\n",
    "    g = f / 11\n",
    "    month = f + 2 - 12 * g\n",
    "    year = 100 * (b - 49) + d + g\n",
    "    DateAndTime = datetime(year=year, month=month, day=day, hour=12, minute=0, second=0)\n",
    "    JTime *= 24\n",
    "    JTime, hours = modf(JTime)\n",
    "    JTime *= 60\n",
    "    JTime, minutes = modf(JTime)\n",
    "    seconds = JTime * 60\n",
    "    DateAndTime += timedelta(hours=hours, minutes=minutes, seconds=seconds)\n",
    "    return DateAndTime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2455198\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2010, 1, 1, 12, 0)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Demo 1\n",
    "t = datetime(year=2010, month=1, day=1, hour=12, minute=0, second=0)\n",
    "print(JulianDate(t))\n",
    "J = Decimal(2455198)\n",
    "RJulianDate(J)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2457023.5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2010, 1, 1, 12, 0)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Demo 2\n",
    "t = datetime(year=2015, month=1, day=1, hour=0, minute=0, second=0)\n",
    "print(JulianDate(t))\n",
    "J = Decimal(2455198)\n",
    "RJulianDate(J)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 当前的问题，datetime类型只能表示公元1年及之后的时间\n",
    "print(RJulianDate(1721426))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('5373484.499988425925925925926')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "JulianDate(datetime(year=9999, month=12, day=31, hour=23, minute=59, second=59))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9999-12-31 12:00:00\n"
     ]
    }
   ],
   "source": [
    "print(RJulianDate(5373484))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from argparse import ArgumentParser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "parser = ArgumentParser('Some description')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "_StoreAction(option_strings=[], dest='integers', nargs='+', const=None, default=None, type=<class 'int'>, choices=None, help='an integer for the accumulator', metavar='N')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parser.add_argument('integers', metavar='N', type=int, nargs='+',\n",
    "                     help='an integer for the accumulator')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dateutil import parser\n",
    "date_obj = parser.parse(\"12-7-2017 13:15:14\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2017, 12, 7, 13, 15, 14)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date_obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pytz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "LOCAL_TIMEZONE = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2018, 10, 26, 17, 2, 7, 721657)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.utcnow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dateutil import parser\n",
    "from datetime import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "default_time = time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "parser.parse()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
